CFLAGS+=-std=c99 -Wall -O2 -DDDB_LYRICS_EXPORT_LIB -D_GNU_SOURCE -fPIC -fvisibility=hidden #-flto=auto
CXXFLAGS+=-std=c++14 -Wall -O2 -fPIC -fvisibility=hidden -DDDB_LYRICS_EXPORT_LIB #-flto=auto
LIBFLAGS=`pkg-config --cflags $(GTK)` -Iinclude
LIBS=`pkg-config --libs $(GTK)`
LCURL=-lcurl
#LDFLAGS+=-flto=auto  # 在MinGW-w64环境中禁用LTO以避免链接问题
GLIBC=glib-compile-resources

prefix ?= $(out)
prefix ?= /usr

GTK=gtk+-3.0 taglib

# 根据操作系统类型设置共享库扩展名
ifeq ($(OS),Windows_NT)
    LYRICBAR=ddb_lyricbar_gtk3.dll
    LYRICDEMO=lyric_demo.exe
else
    LYRICBAR=ddb_lyricbar_gtk3.so
    LYRICDEMO=lyric_demo
endif

lyricbar: resource.h config_dialog.o base64.o LyricUi.o kugou.o LyricTool.o utils.o lyric_log.o resources.o plugin.o
	$(CXX) -shared $(LDFLAGS) plugin.o resources.o config_dialog.o base64.o LyricUi.o kugou.o LyricTool.o utils.o lyric_log.o $(LCURL) -o $(LYRICBAR) $(LIBS)

lyricdemo: resource.h config_dialog.o base64.o LyricUi.o kugou.o LyricTool.o utils.o lyric_log.o resources.o main.o
	$(CXX) $(LDFLAGS) main.o resources.o config_dialog.o base64.o LyricUi.o kugou.o LyricTool.o utils.o lyric_log.o $(LCURL) -o $(LYRICDEMO) $(LIBS)


base64.o: src/base64.cpp src/base64.h
	$(CXX) src/base64.cpp -c $(LIBFLAGS) $(CXXFLAGS) -lcurl

LyricUi.o: src/LyricUi.cpp src/LyricUi.h
	$(CXX) src/LyricUi.cpp -c $(LIBFLAGS) $(CXXFLAGS) -lcurl

kugou.o: src/kugou.cpp src/kugou.h src/json.hpp
	$(CXX) src/kugou.cpp -c $(LIBFLAGS) $(CXXFLAGS) -lcurl

config_dialog.o: src/config_dialog.cpp src/config_dialog.h
	$(CXX) src/config_dialog.cpp -c $(LIBFLAGS) $(CXXFLAGS)

LyricTool.o: src/LyricTool.cpp src/LyricTool.h
	$(CXX) src/LyricTool.cpp -c $(LIBFLAGS) $(CXXFLAGS)

utils.o: src/utils.cpp src/utils.h
	$(CXX) src/utils.cpp -c $(LIBFLAGS) $(CXXFLAGS)

lyric_log.o: src/lyric_log.cpp src/lyric_log.h
	$(CXX) src/lyric_log.cpp -c $(LIBFLAGS) $(CXXFLAGS)

resources.o: src/resources.c
	$(CC) $(CFLAGS) src/resources.c -c $(LIBFLAGS) 

main.o: src/main.cpp src/*.h
	$(CXX) $(CXXFLAGS) src/main.cpp -c $(LIBFLAGS)

plugin.o: src/plugin.cpp src/*.h
	$(CXX) $(CXXFLAGS) src/plugin.cpp -c $(LIBFLAGS)

resource.h:
	$(GLIBC) --generate-source src/resources.xml
	$(GLIBC) --generate-header src/resources.xml

install:
	install -d $(prefix)/lib/deadbeef
	install -d $(prefix)/share/locale/ru/LC_MESSAGES
	install -m 666 -D *.so $(prefix)/lib/deadbeef
	msgfmt gettext/ru/deadbeef-lyricbar.po -o $(prefix)/share/locale/ru/LC_MESSAGES/deadbeef-lyricbar.mo

clean:
ifeq ($(OS),Windows_NT)
		rm -f *.o *.dll *.exe
else
		rm -f *.o *.so
endif
		rm -f resource.h src/resources.c

clean-all: clean
		rm -f $(LYRICBAR) $(LYRICDEMO)

help:
		@echo "Available targets:"
		@echo "  lyricbar    - Build DeaDBeeF plugin (ddb_lyricbar_gtk3.so/.dll)"
		@echo "  lyricdemo   - Build demo application (lyric_demo)"
		@echo "  clean       - Remove object files and generated resources"
		@echo "  clean-all   - Remove all generated files including executables"
		@echo "  install     - Install plugin to system"
		@echo "  help        - Show this help message"
		@echo ""
		@echo "Examples:"
		@echo "  make lyricbar    # Build plugin only"
		@echo "  make lyricdemo   # Build demo only"
		@echo "  make clean       # Clean build files"
		@echo "  make clean-all   # Clean everything"

.PHONY: lyricbar lyricdemo clean clean-all install help
